<template>
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品类型列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图 -->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.productTypeName"
                     clearable @clear="getProductTypeList">
                        <el-button slot="append" icon="el-icon-search" @click="getProductTypeList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="addDialogVisible = true">添加商品类型</el-button>
                </el-col>
            </el-row>
            <!-- 表格 -->
            <el-table :data="productTypeList" stripe>
                <el-table-column type="index" label="序号" width="80"></el-table-column>
                <el-table-column prop="productTypeName" label="商品类型名称" width="120"></el-table-column>
                <el-table-column prop="productTypeDesc" label="描述" width="180"></el-table-column>
                <el-table-column prop="createTime" label="创建日期" width="180"></el-table-column>
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
                            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                            <el-button type="danger" icon="el-icon-delete" size="mini" 
                            @click="removeProductTypeByProductTypeId(scope.row.productTypeId)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.currentPage"
                :page-sizes="[3, 5, 10, 30]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加商品对话框 -->
        <el-dialog title="添加商品类型" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
            <!-- 主体内容区域 -->
            <el-form :model="addForm" :rules="formRules" ref="addFormRef" label-width="80px">
                <el-form-item label="类型名称" prop="productTypeName">
                    <el-input v-model="addForm.productTypeName"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="productTypeDesc">
                    <el-input v-model="addForm.productTypeDesc"></el-input>
                </el-form-item>
            </el-form>
            <!-- 底部区域 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addProductType">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 修改商品对话框 -->
        <el-dialog title="修改商品类型" :visible.sync="editDialogVisible" width="50%" @close="editDialogColosed">
            <el-form :model="editForm" :rules="formRules" ref="editFormRef" label-width="80px">
                <el-form-item label="类型名称" prop="productTypeName">
                    <el-input v-model="editForm.productTypeName"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="productTypeDesc">
                    <el-input v-model="editForm.productTypeDesc"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editProductType">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            //查询条件
            queryInfo: {
                productTypeName: '',
                //当前页数
                currentPage: 1,
                //当前每页显示多少条数据
                pageSize: 5
            },
            //商品类型的数据列表
            productTypeList: [],
            total: 0,
            //控制添加对话框的显示和隐藏
            addDialogVisible: false,
            //添加商品类型的表单数据
            addForm: {
                productTypeName: '',
                productTypeDesc: ''
            },
            //表单验证规则对象
            formRules: {
                productTypeName: [
                    { required: true, message: '请输入类型名称', trigger: 'blur' },
                    { min: 2, max: 15, message: '类型名称在2-15个字符之间', trigger: 'blur' }
                ]
            },
            //控制修改对话框的显示和隐藏
            editDialogVisible: false,
            //修改商品类型的表单数据
            editForm: {}
        }
    },
    created() {
        this.getProductTypeList();
    },
    methods: {
        getProductTypeList() {
            this.$axios
            .get('/productType', { params: this.queryInfo })
            .then(res => {
                const data = res.data;
                if(data.code !== '001') return this.$notifyError("数据获取异常");
                this.productTypeList = data.list;
                this.total = data.totalRecords;
            })
            .catch(err => {
                console.log(err);
            });
        },
        addProductType(){
            //验证表单
            this.$refs.addFormRef.validate(valid => {
                if(!valid) return;
                //发送添加请求
                this.$axios
                .post('/productType',this.addForm)
                .then(res => {
                    const data = res.data;
                    if(data.code !== '001') return this.$notifyError(data.msg);
                    this.$notifySucceed(data.msg);
                    this.addDialogVisible = false;
                    this.getProductTypeList();
                })
                .catch(err => {
                    console.log(err);
                })
            })
        },
        //监听pagesize改变的事件
        handleSizeChange(newSize){
            this.queryInfo.pageSize = newSize;
            this.getProductTypeList();
        },
        //监听 页码值 改变的事件
        handleCurrentChange(newPage){
            this.queryInfo.currentPage = newPage;
            this.getProductTypeList();
        },
        //监听添加对话框的关闭
        addDialogClosed(){
            //重置表单，移除校验结果
            this.$refs.addFormRef.resetFields();
        },
        //监听修改对话框的关闭
        editDialogColosed(){
            //重置表单，移除校验结果
            this.$refs.editFormRef.resetFields();
        },
        //显示修改对话框
        showEditDialog(productType){
            this.editForm = productType;
            this.editDialogVisible = true;
        },
        //修改商品类型
        editProductType(){
            this.$refs.editFormRef.validate(valid => {
                if(!valid) return;
                //发起修改请求
                this.$axios
                .put('/productType', this.editForm)
                .then(res => {
                    const data = res.data;
                    if(data.code !== '001') return this.$notifyError(data.msg);
                    this.$notifySucceed(data.msg);
                    this.editDialogVisible = false;
                    this.getProductTypeList();
                })
                .catch(err => {
                    console.log(err);
                })
            })
        },
        //删除商品类型
        removeProductTypeByProductTypeId(productTypeId){
            console.log(productTypeId);
            this.$confirm('此操作将永久删除该商品类型, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //发起删除请求
                this.$axios
                .delete('/productType/'+productTypeId)
                .then(res => {
                    const data = res.data;
                    if(data.code !== '001') return this.$notifyError(data.msg);
                    this.$notifySucceed(data.msg);
                    this.getProductTypeList();
                })
                .catch(err => {
                    console.log(err);
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        }
    }
}
</script>
<style lang="less" scoped>

</style>